package fr.efrei.international.java.assignment.two;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;

public class Record {

	String currentUser;
	UserRecord ur;
	RandomAccessFile rf;

	 ArrayList<UserRecord> urList = new ArrayList<UserRecord>();

	public Record(String currentUser) {
		this.currentUser = currentUser;
		try {
			rf = new RandomAccessFile(".\\user information\\" + currentUser
					+ ".txt", "rw");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void recordInformation(UserRecord ur) {
		this.ur = ur;
		try {
			rf = new RandomAccessFile(".\\user information\\" + currentUser
					+ ".txt", "rw");
			rf.seek(rf.length());
			rf.writeBytes(String.valueOf(ur.id));
			rf.writeBytes("	");
			rf.writeBytes(String.valueOf(ur.hasKnown));
			rf.writeBytes("	");
			rf.writeBytes(ur.currentTime + "\r\n");
			rf.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public ArrayList<UserRecord> readRecord() {
		try {
			FileReader fr = new FileReader(".\\user information\\"
					+ currentUser+".txt");
			BufferedReader br = new BufferedReader(fr);
			String str1 = null;
			String[] s1 = null;
			while ((str1 = br.readLine()) != null) {
				s1 = str1.split("	");
				UserRecord ur = new UserRecord(s1[2], Boolean.valueOf(s1[1]), Integer.valueOf(s1[0]));
				urList.add(ur);
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return urList;
	}

}
